package com.dianban.controller;

import com.dianban.base.ResultCode;
import com.dianban.base.ResultVo;
import com.dianban.config.redis.RedisConstant;
import com.dianban.config.redis.RedisUtil;
import com.dianban.entity.ShopUser;
import com.dianban.entity.User;
import com.dianban.service.ShopUserService;
import com.dianban.service.UserService;
import com.dianban.utils.IdGen;
import com.dianban.utils.MD5;
import com.dianban.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wujh
 * @create 2020/1/10
 */
@Api(value = "ApiShopUserController", description = "商户用户控制器")
@RestController
@RequestMapping(value = "${interfacePath}/shopUser/")
public class ApiShopUserController extends BaseController {

    @Autowired
    private ShopUserService shopUserService;

    @ApiOperation(notes = "login", httpMethod = "POST", value = "商户用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "screenMac", value = "屏幕Mac", required = true, paramType = "header",dataType = "string"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "query",dataType = "string"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query",dataType = "string"),
    })
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public ResultVo login(String mobile , String password, HttpServletRequest request, HttpServletResponse response) {
        if(StringUtils.isEmpty(mobile)){
            return ResultVo.error(ResultCode.ERROR,"手机号不能为空");
        }
        if(StringUtils.isEmpty(password)){
            return ResultVo.error(ResultCode.ERROR,"密码不能为空");
        }
        ShopUser shopUser = shopUserService.findByMobile(mobile);
        if(shopUser == null){
            return ResultVo.error(ResultCode.ERROR,"手机号不存在");
        }
        if(!"1".equals(shopUser.getStatus())){
            return ResultVo.error(ResultCode.ERROR,"该手机号已被禁用");
        }
        String passwordMd5 = MD5.parseMD5(MD5.parseMD5(password)+shopUser.getSalt());
        if(!passwordMd5.equals(shopUser.getPassword())){
            return ResultVo.error(ResultCode.ERROR,"密码错误，请重新输入");
        }
        Map<String,Object> resultMap = new HashMap<>();
        String shopUserToken = IdGen.uuid();
        shopUser.setShopUserToken(shopUserToken);
        String key = RedisConstant.SHOP_USER_TOKEN_KEY+shopUserToken;
        RedisUtil.set(key,shopUser);
        resultMap.put("shopUser",shopUser);
        return ResultVo.success(resultMap,"成功");
    }

    @ApiOperation(notes = "logout", httpMethod = "GET", value = "商户用户登出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "screenMac", value = "屏幕Mac", required = true, paramType = "header",dataType = "string"),
            @ApiImplicitParam(name = "shopUserToken", value = "商户用户token", required = true, paramType = "header", dataType = "string"),
    })
    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public ResultVo logout(HttpServletRequest request, HttpServletResponse response) {
        UserUtils.logoutShopUser(request);
        return ResultVo.success("", "操作成功");
    }

}
